package com.ecity.table 
{
	/**
	 * ...
	 * @author yicheng.zhang
	 */
	import com.ecity.debug.Debug;
	import com.ecity.events.GunEvent;
	import com.ecity.util.BitmapClip;
	import com.ecity.util.UIFactory;
	import flash.display.Bitmap;
	import flash.display.SpreadMethod;
	import flash.display.Sprite;
	import flash.events.Event;
	import gs.TweenLite;
	import gs.easing.*;
	
	public class Gun extends TableDisplayObject	{
		private var gun:Bitmap;
		private var owheel:OcelotWheel;
		private var callFireBg:BitmapClip;
		
		private var layer:Sprite;
		private var fireCallerLayer:Sprite;
		private var wheellayer:Sprite;
		
		private var angle:int;
		public function Gun(){
			gun = UIFactory.getBitmapbyName("gun") as Bitmap;
			gun.x = -1*gun.width / 2;
			gun.y = -1*gun.height / 2;
			layer = getLayer("gun");
			layer.addChild(gun);
			layer.visible = false;
			initOcelotWheel();
		}
		
		private function initOcelotWheel():void {
			owheel = new OcelotWheel();
			owheel.x = 500;
			owheel.y = 400;
			owheel.addEventListener(GunEvent.WHEEL_ISREADY, onWheelisReady);
			owheel.addEventListener(GunEvent.SPIN_COMPLETE, onSpinComplete);
			wheellayer = this.getLayer("wheel");
			wheellayer.addChild(owheel);
			initCallFire();
			
		}
		
		private function initCallFire():void {
			callFireBg = new BitmapClip("callfirebg", 4);
			callFireBg.play();
			callFireBg.x = -81.5;
			callFireBg.y = -81.5;
			fireCallerLayer = this.getLayer("fireCaller");
			fireCallerLayer.addChild(callFireBg);
			fireCallerLayer.x = 500;
			fireCallerLayer.y = 400;
			fireCallerLayer.visible = false;
		}
		
		private function onWheelisReady(e:GunEvent):void {
			this.dispatchEvent(new GunEvent(GunEvent.WHEEL_ISREADY));
		}
		private function onSpinComplete(e:GunEvent):void {
			this.dispatchEvent(new GunEvent(GunEvent.SPIN_COMPLETE));
		}
		
		public function loadAndSpin():void {
			owheel.loadBullet();
		}
		public function moveToPlayer(player:TableDisplayObject):void {
			layer.rotation = player.rotation;
			var mathAngle:Number = player.rotation * (Math.PI / 180);
			var headX:Number = player.x + 60 * Math.sin(mathAngle);
			var headY:Number = player.y - 60 * Math.cos(mathAngle);
			layer.x = headX + 500 * Math.cos(mathAngle);
			layer.y = headY + 500 * Math.sin(mathAngle);
			var mX:Number = headX + 200 * Math.cos(mathAngle);
			var mY:Number = headY + 200 * Math.sin(mathAngle);
			layer.visible = true;
			TweenLite.to(layer, 1, { x:mX, y:mY, onComplete:onFinishMove, onCompleteParams:[player] } );
		}
		
		private function onFinishMove(player:TableDisplayObject):void {
			angle = player.rotation + 360;
			TweenLite.to(layer, 0.5, {rotation:angle,onComplete:onFinishRoll,onCompleteParams:[player]});
		}
		
		/**
		 * Aim the player's head,more closer to the head;
		 * @param	postion
		 */
		private function onFinishRoll(player:TableDisplayObject):void {
			var mathAngle:Number = player.rotation * (Math.PI / 180);
			var headX:Number = player.x + 60 * Math.sin(mathAngle);
			var headY:Number = player.y - 60 * Math.cos(mathAngle);
			var mX:Number = headX + 150 * Math.cos(mathAngle);
			var mY:Number = headY + 150 * Math.sin(mathAngle);
			this.dispatchEvent(new GunEvent(GunEvent.AIM_COMPLETE));
			fireCallerLayer.visible = true;
			//TweenLite.to(layer, 0.5, { x:mX, y:mY, onComplete:onFire} );
		}
				
		public function onFire():void {
			var isExplode:Boolean = owheel.fire();
			fireCallerLayer.visible = false;
			this.dispatchEvent(new GunEvent(GunEvent.FIRE_COMPLETE, {isExplode:isExplode} ));
		}
		

		
		
		
	}

}